#include "fontsettings.h"
#include "ui_fontsettings.h"
#include <QComboBox>
#include <QDialog>
#include <QFontDialog>


FontSettings::FontSettings(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FontSettings)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);
    setWindowFlag(Qt::WindowStaysOnTopHint);
    move(1200, 100);

    //添加颜色
    pa.setColor(QPalette::Background, Qt::black);
    ui->selcolor->setAutoFillBackground(true);
    ui->selcolor->setPalette(pa);
    //ui->selcolor->setStyleSheet("QLabel{background-color:rgb(0,0,0);}"); 设置颜色后不可改变
    ui->color1->setStyleSheet("QLabel{background-color:rgb(0,0,0);}");
    ui->color2->setStyleSheet("QLabel{background-color:rgb(255,0,0);}");
    ui->color3->setStyleSheet("QLabel{background-color:rgb(0,255,0);}");
    ui->color4->setStyleSheet("QLabel{background-color:rgb(200,101,102);}");
    ui->color5->setStyleSheet("QLabel{background-color:rgb(0,0,255);}");
    ui->color6->setStyleSheet("QLabel{background-color:rgb(255,255,0);}");
    ui->color7->setStyleSheet("QLabel{background-color:rgb(255,0,255);}");
    ui->color8->setStyleSheet("QLabel{background-color:rgb(0,255,255);}");
    //添加点击事件
    connect(ui->fontType, &QComboBox::currentTextChanged, [=](){
         QString type = ui->fontType->currentText();//获取当前文本
         int flag = 0;
         QFont f;
         if (QString::compare(type, "自定义") == 0) {
             bool k;
             flag = 1;
             f = QFontDialog::getFont(&k);
             //发送给画板界面
         }
         if (flag == 0) {
             f.setFamily(type);
         }
         sendFont(f);
    });
    connect(ui->fontSize, &QComboBox::currentTextChanged, [=](){
        QString size_ = ui->fontSize->currentText();
        //发送给画板界面
        bool ok;
        int Size = size_.toInt(&ok,10);
        sendSize(Size);
    });
    //标签安装事件过滤器
    ui->color1->installEventFilter(this);
    ui->color2->installEventFilter(this);
    ui->color3->installEventFilter(this);
    ui->color4->installEventFilter(this);
    ui->color5->installEventFilter(this);
    ui->color6->installEventFilter(this);
    ui->color7->installEventFilter(this);
    ui->color8->installEventFilter(this);

}

FontSettings::~FontSettings()
{
    delete ui;
}

//标签的点击事件
bool FontSettings::eventFilter(QObject *obj, QEvent *e)
{
    if (e->type() == QEvent::MouseButtonPress) {
        if (obj == ui->color1) {
            pa.setColor(QPalette::Background, QColor(0, 0, 0));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(0, 0, 0);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color2) {
            pa.setColor(QPalette::Background, QColor(255, 0, 0));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(255, 0, 0);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color3) {
            pa.setColor(QPalette::Background, QColor(0, 255, 0));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(0, 255, 0);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color4) {
            pa.setColor(QPalette::Background, QColor(200, 101, 102));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(200, 101, 102);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color5) {
            pa.setColor(QPalette::Background, QColor(0, 0, 255));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(0, 0, 255);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color6) {
            pa.setColor(QPalette::Background, QColor(255, 255, 0));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(255, 255, 0);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color7) {
            pa.setColor(QPalette::Background, QColor(255, 0, 255));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(255, 0, 255);
            emit sendFontColor(fontColor);
            return true;
        }
        if (obj == ui->color8) {
            pa.setColor(QPalette::Background, QColor(0, 255, 255));
            ui->selcolor->setPalette(pa);
            fontColor = QColor(0, 255, 255);
            emit sendFontColor(fontColor);
            return true;
        }
    }
}

//选择字体的颜色
void FontSettings::on_chose_clicked()
{
    fontColor = QColorDialog::getColor(Qt::black, this, tr("选择字体颜色"));
    pa.setColor(QPalette::Background, fontColor);
    ui->selcolor->setPalette(pa);
    emit sendFontColor(fontColor);
}

void FontSettings::sendSize(int size_)
{
    emit sendFontSize(size_);
}

void FontSettings::sendFont(QFont fFont)
{
    emit sendFontType(fFont);
}

